import sys
import re

# 基础功能（v0.2保留）
def count_chars(content):
    return len(content)

def count_words(content):
    return len(re.findall(r'\b[a-zA-Z]+\b', content))

def count_sentences(content):
    sentences = re.split(r'[.!?]', content)
    return len([s for s in sentences if s.strip()])

# 扩展功能（v0.3新增）
def count_lines(content):
    """统计总行数"""
    return len(content.split('\n'))

def count_blank_lines(content):
    """统计空行数（仅空格/制表符/换行的行）"""
    lines = content.split('\n')
    return len([line for line in lines if line.strip() == ''])

def count_comment_lines(content):
    """统计注释行数（#开头的行）"""
    lines = content.split('\n')
    return len([line for line in lines if line.strip().startswith('#')])

def count_code_lines(content):
    """统计代码行数（非空非注释行）"""
    lines = content.split('\n')
    return len([line for line in lines if line.strip() and not line.strip().startswith('#')])

# 主函数（支持基础+扩展参数）
def main():
    if len(sys.argv) != 3:
        print("用法: python wc.py [参数] [文件名]")
        print("基础参数: -c(字符数), -w(单词数), -s(句子数)")
        print("扩展参数: -l(总行数), -b(空行), -com(注释行), -code(代码行), -all(全部统计)")
        return
    
    param, filename = sys.argv[1], sys.argv[2]
    
    try:
        with open(filename, 'r', encoding='utf-8') as f:
            content = f.read()
    except FileNotFoundError:
        print(f"错误: 文件 '{filename}' 不存在")
        return
    
    # 处理参数
    if param == '-c':
        print(f"字符数: {count_chars(content)}")
    elif param == '-w':
        print(f"单词数: {count_words(content)}")
    elif param == '-s':
        print(f"句子数: {count_sentences(content)}")
    elif param == '-l':
        print(f"总行数: {count_lines(content)}")
    elif param == '-b':
        print(f"空行数: {count_blank_lines(content)}")
    elif param == '-com':
        print(f"注释行数: {count_comment_lines(content)}")
    elif param == '-code':
        print(f"代码行数: {count_code_lines(content)}")
    elif param == '-all':
        print(f"文件: {filename}")
        print(f"字符数: {count_chars(content)}")
        print(f"单词数: {count_words(content)}")
        print(f"句子数: {count_sentences(content)}")
        print(f"总行数: {count_lines(content)}")
        print(f"空行数: {count_blank_lines(content)}")
        print(f"注释行数: {count_comment_lines(content)}")
        print(f"代码行数: {count_code_lines(content)}")
    else:
        print(f"错误: 未知参数 '{param}'")

if __name__ == "__main__":
    main()